package com.mbigger.redis;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Coffee> coffeeRedisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Coffee> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        configTemplate(template, Coffee.class);
        return template;
    }

    @Bean
    public RedisTemplate<String, RtValue> rtValueTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, RtValue> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        configTemplate(template, RtValue.class);
        return template;
    }

    @Bean
    public RedisTemplate<String, User> userRedisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, User> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        configTemplate(template, User.class);
        return template;
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        configTemplate(template, Object.class);
        return template;
    }

    /*private <T> void  configTemplate(RedisTemplate<String, T> template){
        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        RedisSerializer<?> stringSerializer = new StringRedisSerializer();
        // 如果key是String 需要配置一下StringSerializer,不然key会乱码
        template.setKeySerializer(stringSerializer);// key序列化
        template.setValueSerializer(genericJackson2JsonRedisSerializer);// value序列化
        template.setHashKeySerializer(stringSerializer);// Hash key序列化
        template.setHashValueSerializer(genericJackson2JsonRedisSerializer);// Hash value序列化
        template.afterPropertiesSet();
    }*/

    private <T> void configTemplate(RedisTemplate<String, T> template, Class<T> clazz) {
        Jackson2JsonRedisSerializer<T> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(clazz);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(mapper);
        RedisSerializer<?> stringSerializer = new StringRedisSerializer();
        // 如果key是String 需要配置一下StringSerializer,不然key会乱码
        template.setKeySerializer(stringSerializer);// key序列化
        template.setValueSerializer(jackson2JsonRedisSerializer);// value序列化
        template.setHashKeySerializer(stringSerializer);// Hash key序列化
        template.setHashValueSerializer(jackson2JsonRedisSerializer);// Hash value序列化
        template.afterPropertiesSet();
    }
}
